package com.izkml.bookfilp;

import android.os.Bundle;
import android.view.MotionEvent;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager2.widget.ViewPager2;

import com.blankj.utilcode.util.ImageUtils;
import com.izkml.bookfilp.adapter.PagerAdapter;
import com.izkml.bookfilp.anim.PageAnimation;
import com.izkml.bookfilp.databinding.ActivityMainBinding;
import com.izkml.bookfilp.view.PageOverlayView2;

import java.util.Objects;

public class MainActivity extends AppCompatActivity {

    private ActivityMainBinding binding;
    private PagerAdapter adapter;
    private PageOverlayView2 pageOverlayView;
    private PageAnimation.OnPageChangeListener onPageChangeListener = new PageAnimation.OnPageChangeListener() {
        @Override
        public boolean hasPrev() {
            return true;
        }

        @Override
        public boolean hasNext() {
            return true;
        }

        @Override
        public void pageCancel() {

        }

        @Override
        public void next() {
            binding.vp2.setCurrentItem(pageOverlayView.getCurrentPosition() + 1);
        }

        @Override
        public void prev() {
            binding.vp2.setCurrentItem(pageOverlayView.getCurrentPosition() - 1);
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        pageOverlayView = new PageOverlayView2(this, onPageChangeListener);
        binding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        adapter = new PagerAdapter(this);
        binding.vp2.setAdapter(adapter);
        binding.vp2.setPageTransformer((page, position) -> {
            if (position < 0f) {
                page.setTranslationX(0f);
                page.setTranslationZ(0f);
            } else {
                page.setTranslationX(page.getWidth() * -position);
                page.setTranslationZ(-position);
            }
        });
        binding.vp2.setUserInputEnabled(false);
        binding.vp2.setOffscreenPageLimit(5);
        binding.vp2.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                super.onPageScrolled(position, positionOffset, positionOffsetPixels);
                pos = position;
                if (position != pageOverlayView.getCurrentPosition()) {
                    if (canSet) {
                        pageOverlayView.setBitmaps(
                                position < 1 ? null :
                                        ImageUtils.view2Bitmap(Objects.requireNonNull(((RecyclerView) binding.vp2.getChildAt(0)).findViewHolderForAdapterPosition(position - 1)).itemView),
                                ImageUtils.view2Bitmap(Objects.requireNonNull(((RecyclerView) binding.vp2.getChildAt(0)).findViewHolderForAdapterPosition(position)).itemView),
                                ImageUtils.view2Bitmap(Objects.requireNonNull(((RecyclerView) binding.vp2.getChildAt(0)).findViewHolderForAdapterPosition(position + 1)).itemView)
                        );
                    }
                    canSet = false;
                }
                pageOverlayView.setCurrentPosition(position);
                pageOverlayView.setCurrentPositionOffsetPx(positionOffsetPixels);
            }

            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
                pageOverlayView.layout(
                        binding.vp2.getLeft(),
                        binding.vp2.getTop(),
                        binding.vp2.getRight(),
                        binding.vp2.getBottom()
                );
            }

            @Override
            public void onPageScrollStateChanged(int state) {
                super.onPageScrollStateChanged(state);
                if (state == 0) {
                    binding.vp2.getOverlay().remove(pageOverlayView);
                    pageOverlayView.setBitmaps(
                            pos < 1 ? null :
                                    ImageUtils.view2Bitmap(Objects.requireNonNull(((RecyclerView) binding.vp2.getChildAt(0)).findViewHolderForAdapterPosition(pos - 1)).itemView),
                            ImageUtils.view2Bitmap(Objects.requireNonNull(((RecyclerView) binding.vp2.getChildAt(0)).findViewHolderForAdapterPosition(pos)).itemView),
                            ImageUtils.view2Bitmap(Objects.requireNonNull(((RecyclerView) binding.vp2.getChildAt(0)).findViewHolderForAdapterPosition(pos + 1)).itemView)
                    );
                }
            }
        });
    }

    private boolean canSet = true;
    private int pos = 0;

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        if (ev.getAction() == MotionEvent.ACTION_DOWN) {
            //canSet = false;
            binding.vp2.getOverlay().add(pageOverlayView);
        } else if (ev.getAction() == MotionEvent.ACTION_UP) {
            //ThreadUtils.runOnUiThreadDelayed(() -> binding.vp2.getOverlay().remove(pageOverlayView), 200);
            //canSet = true;
        }
        pageOverlayView.onTouchEvent2(ev);
        return super.dispatchTouchEvent(ev);
    }
}